package org.uim.cricket.afetcher.exec.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.List;

import org.uim.cricket.afetcher.AFlogger;

public class RuntimeExecutor {
	
	public boolean execute(List<String> commands, String workDir, String[] envVars) throws Exception {
		
		ProcessBuilder pb = new ProcessBuilder();
		pb.command(commands);
		pb.redirectErrorStream(true);
		pb.directory(new File(workDir));
		Process p = pb.start();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line = null;
		while ((line = reader.readLine()) != null) {
			//System.out.println(line);
		}
		line = "OK";
		line = line.substring(0,1);
		reader.close();
		
		if (p.exitValue() != 0) {
			AFlogger.error(" Delegated process didn't successfully completed!");
			return false;
		} else {
			AFlogger.debug(" Successfully completed!");
			return true;
		}

	}

}
